from tkinter import *
from tkinter.ttk import *
app = Tk()
app.title("Single JSL")
app.geometry("300x200")
app.config(bg="#f5f5dc")
app.resizable(False, False)
style=Style()
style.configure(".", font=("Arial", 24), foreground="red", background="#f5f5dc")
S1=Style()
S1.configure("TLabel",font=('Helvetica', 12,'bold'))

Label(app, text="NAME").place(x=10, y=5)
Label(app, text="USL").place(x=10, y=25)
Label(app, text="LSL").place(x=10, y=45)
Label(app, text="NORMAL").place(x=10, y=65)
Label(app, text="X").place(x=10, y=85)
Label(app, text="X group").place(x=10, y=105)
Label(app, text="Y group").place(x=10, y=125)
e1=Entry(app,width=20)
e1.place(x=90, y=5)
e2=Entry(app,width=20)
e2.place(x=90, y=25)
e3=Entry(app,width=20)
e3.place(x=90, y=45)
e4=Entry(app,width=20)
e4.place(x=90, y=65)
e5=Entry(app,width=20)
e5.place(x=90, y=85)
e6=Entry(app,width=20)
e6.place(x=90, y=105)
e7=Entry(app,width=20)
e7.place(x=90, y=125)
def get_arry(name):
    str1=name.split(",")
    return str1
def get_name(name):
    str1=name.split(",")
    str2=str1[0].split("-")
    return str2[0]
def jsl(e1,e2,e3,e4,e5,e6,e7):
    name=e1.get()
    usl=e2.get()
    lsl=e3.get()
    normal=e4.get()
    x=e5.get()
    x_group=e6.get()
    y_group=e7.get()
    if x_group=="":
        x_g=""
    else:
        x_g=',Group X(:'+x_group+')'
    if y_group=="":
        y_g=""
    else:
        y_g=',Group Y(:'+y_group+')'
    s='Open("$data/JMP.xlsx");'
    f=""
    if lsl !='':
        MIN=float(lsl)-0.02
        f=f+str(MIN)+'), Max( '
        if usl !='':
            MAX=float(usl)+0.02
            f=f+str(MAX)+'), Inc( 0.01 ), Minor Ticks( 0 ),\nAdd Ref Line('+normal+', "Dashed", "Black", "Nominal '+normal+'", 1 ),\nAdd Ref Line('+usl+', "Dashed", "Red", "USL '+usl+'", 1 ),\nAdd Ref Line('+lsl+', "Dashed", "Red", "LSL '+lsl+'", 1 )}\n),\nDispatch( {}, "graph title", TextEditBox, {Set Text( "'+get_name(name)+'" )} )\n),\nsave picture("'+get_name(name)+'.png","png")\n);'
        else:
            MAX=float(normal)+0.02
            f=f+str(MAX)+'), Inc( 0.01 ), Minor Ticks( 0 ),\nAdd Ref Line('+normal+', "Dashed", "Black", "Nominal '+normal+'", 1 ),\nAdd Ref Line('+lsl+', "Dashed", "Red", "LSL '+lsl+'", 1 )}\n),\nDispatch( {}, "graph title", TextEditBox, {Set Text( "'+get_name(name)+'" )} )\n),\nsave picture("'+get_name(name)+'.png","png")\n);'
    else:
        MIN=float(normal)-0.02
        f=f+str(MIN)+'), Max( '
        if usl !='':
            MAX=float(usl)+0.02
            f=f+str(MAX)+'), Inc( 0.01 ), Minor Ticks( 0 ),\nAdd Ref Line('+normal+', "Dashed", "Black", "Nominal '+normal+'", 1 ),\nAdd Ref Line('+usl+', "Dashed", "Red", "USL '+usl+'", 1 )}\n),\nDispatch( {}, "graph title", TextEditBox, {Set Text( "'+get_name(name)+'" )} )\n),\nsave picture("'+get_name(name)+'.png","png")\n);'
    if "," in name:#多乘
        s=s+'Data Table( "data" ) << Stack(\ncolumns('
        for i in get_arry(name):
            s=s+':Name("'+i+'"),'
        s=s+'),\nSource Label Column( "'+get_name(name)+'"),\nStacked Data Column( "'+get_name(name)+' Data" ),\nOutput Table( "'+get_name(name)+'")\n);\n'+'Graph Builder(\nSize( 800,600 ),\nShow Control Panel( 0 ),\nVariables(\nX( :'
        if x=="":
            s=s+get_name(name)+' ),\nY( :'+get_name(name)+' Data)'+x_g+y_g+',Color( :'+get_name(name)+')),\nElements(\nPoints( X, Y, Legend( 18 ), Jitter( "Random Normal" ) ),\nBox Plot( X, Y, Legend( 19 ), Jitter( "Random Normal" ) )\n),\nSendToReport(\nDispatch(\n{},\n"'
            s=s+get_name(name)+' Data", \nScale Box, \n{ Format( "Fixed Dec", 15, 2 ), Min('
        else:#x不为空
            s=s+x+'),\nX( :'+get_name(name)+', Position( 1 ) ),\nY( :'+get_name(name)+' Data)'+x_g+y_g+',Color( :'+get_name(name)+')),\nElements(\nPoints( X(1),X(2), Y, Legend( 18 ), Jitter( "Random Normal" ) ),\nBox Plot( X(1),X(2), Y, Legend( 19 ), Jitter( "Random Normal" ) )\n),\nSendToReport(\nDispatch(\n{},\n"'
            s=s+get_name(name)+' Data", \nScale Box, \n{ Format( "Fixed Dec", 15, 2 ), Min('
    else:#单乘
        if x=="":
            s='Graph Builder(\nSize( 800,600 ),\nShow Control Panel( 0 ),\nVariables( Y( :'+name+')'+x_g+y_g+'),\nElements(\nPoints( Y, Legend( 7 ), Jitter( "Random Normal" ) ),\nBox Plot(Y, Legend( 8 ), Jitter(  "Random Normal" ) )\n),\nSendToReport(\nDispatch(\n{},\n"'+name+'",\nScaleBox,{Format( "Fixed Dec", 15, 2 ),Min('
        else:
            s='Graph Builder(\nSize( 800,600 ),\nShow Control Panel( 0 ),\nVariables( X( :'+x+'), Y( :'+name+')'+x_g+y_g+'),\nElements(\nPoints( X,Y, Legend( 7 ), Jitter( "Random Normal" ) ),\nBox Plot(X,Y, Legend( 8 ), Jitter(  "Random Normal" ) )\n),\nSendToReport(\nDispatch(\n{},\n"'+name+'",\nScaleBox,{Format( "Fixed Dec", 15, 2 ),Min('
    s=s+f
    f = open("d:/new.jsl", "w")
    f.write(s)
    f.close()
btn=Button(app, text="Start",command=lambda:jsl(e1,e2,e3,e4,e5,e6,e7))
btn.place(x=45, y=150)
app.mainloop()